﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 依赖倒置原则._3
{
    public interface ICar
    {
        void Run();
    }
    public class Benz : ICar
    {
        public void Run()
        {
            Console.WriteLine("奔驰汽车在本跑");
        }
    }
    public class AoDi : ICar
    {
        public void Run()
        {
            Console.WriteLine("奥迪汽车在本跑");
        }
    }


    public interface IDriver
    {
        //在接口或类中 将要注入的服务对象，以参数的形式直接注入，我们称之为接口注入；
        void Drive(ICar car);
        void Drive();
        void SetCar(ICar car);
    }
    public class Student : IDriver
    {
        private ICar _car;
       /// <summary>
       /// 接口注入
       /// </summary>
       /// <param name="car"></param>
        public void Drive(ICar car)
        {
          car.Run();
        }

        public void Drive()
        {
            _car.Run();
        }
        /// <summary>
        /// 属性注入
        /// </summary>
        /// <param name="car"></param>
        public void SetCar(ICar car)
        {
            _car=car;   
        }
    }
    public class Teacher : IDriver
    {
        private readonly ICar _car;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="car"></param>
        public Teacher(ICar car)
        {
            _car = car;
        }
        public void Drive(ICar car)
        {
            throw new NotImplementedException();
        }
        public void SetCar(ICar car)
        {
            throw new NotImplementedException();
        }
        public void Drive()
        {
            _car.Run();
        }
    }
}
